package org.parademo.web.infra;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Wraps an HandlerExceptionResolver and logs the exception before delegation.
 */
public class LoggingExceptionResolver implements HandlerExceptionResolver {
    private static final Logger log = LoggerFactory.getLogger(LoggingExceptionResolver.class);

    private final HandlerExceptionResolver wrappedResolver;

    public LoggingExceptionResolver(HandlerExceptionResolver wrappedResolver) {
        this.wrappedResolver = wrappedResolver;
    }

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        log.error("Error occurred while executing handler [" + handler + "]", ex);

        return wrappedResolver.resolveException(request, response, handler, ex);
    }
}
